package com.health.es.generate;

import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;

import java.util.StringJoiner;

/**
 * @author nqlz
 * @description ：字段生成辅助类
 */
@Slf4j
public class GenerateCodeUtil {

    /**
     * 字段生成分隔符
     */
    public static final String delimiter = "Ø";


    public static String enCodeByDelimiter(Object object, String... keys) {
        JSONObject json = (JSONObject) JSONObject.toJSON(object);
        StringJoiner result = new StringJoiner(delimiter);
        for (String v : keys) {
            if (null == json.get(v)) {
                String msg = "表：" + object.getClass().getName() + " 插入错误, 字段" + v + ":为空";
                log.error(msg);
                throw new RuntimeException(msg);
            }
            String s = json.get(v).toString();
            result.add(s);
        }
        return result.toString();
    }
    public static String enCodeByDelimiter(JSONObject json,String className, String... keys) {
        StringJoiner result = new StringJoiner(delimiter);
        for (String v : keys) {
            if (null == json.get(v)) {
                String msg = "表：" + className + " 插入错误, 字段" + v + ":为空";
                log.error(msg);
                throw new RuntimeException(msg);
            }
            String s = json.get(v).toString();
            result.add(s);
        }
        return result.toString();
    }


}
